package me.gacl.beetl.web.listener;


import org.beetl.core.resource.WebAppResourceLoader;
import org.beetl.ext.servlet.ServletGroupTemplate;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * BeetlServletGroupTemplate初始化监听器
 */
@WebListener
public class BeetlServletGroupTemplateListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //获取项目根目录路径
        String root = servletContextEvent.getServletContext().getRealPath("/");
        System.out.println("项目根目录路径root=" + root);
        //根据项目根目录路径创建一个WebAppResourceLoader
        WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(root, "UTF-8");
        //重新设置ResourceLoader
        ServletGroupTemplate.instance().getGroupTemplate().setResourceLoader(webAppResourceLoader);
        System.out.println("-----系统初始化------");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("-----系统销毁------");
    }
}
